<template>

    <a-form layout='horizontal' class="userEditRoot">
        <a-form-item label="用户账号"
        >
            <a-input v-model="form.userName" :disabled="true"></a-input>
        </a-form-item>

        <a-form-item label="是否更新密码">
            <a-radio-group v-model="userNeedUpdatePassword">
                <a-radio-button :value="true">是</a-radio-button>
                <a-radio-button :value="false">否</a-radio-button>
            </a-radio-group>
        </a-form-item>

        <a-form-item label="用户密码" v-if="userNeedUpdatePassword"
        >
            <a-input v-model="form.userPassword" type="password"></a-input>
        </a-form-item>

        <a-form-item label="用户昵称"
        >
            <a-input v-model="form.userNickname" ></a-input>
        </a-form-item>

        <a-form-item label="部门">
            <a-tree-select v-model="form.userDepartment" :treeData="departmentTreeData" placeholder="请选择所属部门" treeDefaultExpandAll></a-tree-select>
        </a-form-item>

        <a-form-item label="账号是否会过期">
            <a-radio-group v-model="userNeedExpire">
                <a-radio-button  :value="true">是</a-radio-button>
                <a-radio-button  :value="false">否</a-radio-button>
            </a-radio-group>
        </a-form-item>

        <a-form-item label="过期时间"
                     v-if="userNeedExpire">
            <a-date-picker showTime  v-model="form.userExpire" format="YYYY-MM-DD hh:mm:ss"></a-date-picker>
        </a-form-item>

        <a-form-item label="账号状态">
            <a-radio-group v-model="form.userLock">
                <a-radio-button :value="true">冻结</a-radio-button>
                <a-radio-button :value="false">正常</a-radio-button>
            </a-radio-group>
        </a-form-item>

    </a-form>
</template>

<script>

    import moment from "moment";
    import _ from 'lodash';
    import {Form, DatePicker,Input,Button,Icon,Radio,TreeSelect,message} from 'ant-design-vue';

    export default {
        name: "UserEdit",
        components: {
            ARadioButton:Radio.Button,
            ARadioGroup:Radio.Group,
            AFormItem:Form.Item,
            AForm:Form,
            ADatePicker:DatePicker,
            AInput:Input,
            AButton:Button,
            AIcon:Icon,
            ATreeSelect:TreeSelect
        },
        props: ['value'],
        data() {
            return {
                form: {
                    userName: '',
                    userPassword: '',
                    userExpire: '',
                    userNickname:'',
                    userLock:'',
                    userDepartment: ''
                },
                userNeedExpire: false,
                userNeedUpdatePassword:false,
                departmentTreeData:[]
            };
        },
        mounted(){

            this.loadUser();
            this.loadDepartments();
        },
        methods: {
            loadDepartments(){
                this.$axios.get("/auth/department/getDepartmentList")
                    .then(d=>{
                        if(d.data.status == 0){
                            let data = d.data.data;

                            this.departmentTreeData = this.buildTreeSelectData(data);
                        }

                    })
                    .catch(e=>{
                        message.error("部门信息加载失败");
                    })
            },

            buildTreeSelectData(data){
                let result = [];
                for(let item of data){

                    let node = {
                        title:item.department.departmentName,
                        value:item.department.departmentId
                    };

                    if(item.children && item.children.length > 0){
                        node.children = this.buildTreeSelectData(item.children);
                    }
                    result.push(node);
                }

                return result;
            },
            verfity() {

                if (this.form.userName === '') {
                    message.error('请输入用户账号');
                    return false;
                }
                if (this.form.userPassword === '') {
                    message.error('请输入用户密码');
                    return false;
                }
                if (this.form.userNickname === '') {
                    message.error('请输入用户昵称');
                    return false;
                }
                if(this.form.userDepartment === ''){
                    message.error('请选择所属部门');
                    return false;
                }
                if (this.userNeedExpire) {

                    if (this.form.userExpire === '' || this.form.userExpire == null || this.form.userExpire === undefined) {
                        message.error('请输入用户过期时间');
                        return false;
                    }

                }

                return true;

            },

            submitData() {

                if (this.verfity()) {


                    let formData = _.cloneDeep(this.form);

                    if(this.userNeedExpire){
                        formData.userExpire = formData.userExpire.format("YYYY-MM-DD hh:mm:ss");
                    }else{
                        delete formData.userExpire;
                    }

                    this.$axios.put("/auth/user/updateUser?updatePassword=" + this.userNeedUpdatePassword, formData)
                        .then(d => {

                            let result = d.data;

                            if (result.status == 0) {
                                message.success('更新成功');
                                this.$emit("ok");

                            } else {
                                message.error(result.message);
                            }
                        })
                        .catch(e => {
                            message.error('更新失败,未知错误');
                        })

                }


            },

            loadUser(){
                if(this.value && this.value != ''){

                    this.$axios.get("/auth/user/getUser?userId=" + this.value)
                        .then(d=>{
                            let result = d.data;
                            if(result.status === 0 ){
                                this.form = result.data;
                                this.userNeedExpire = (this.form.userExpire != '' || this.form.userExpire)
                            }
                        })
                        .catch(e=>{
                            message.error("用户数据加载失败");
                        })
                }
            }
        },
        watch:{
            'form.userNeedExpire'(){
                if(!this.form.userNeedExpire){
                    this.form.userExpire = '';
                }
            }
        }
    }
</script>

<style scoped>

    .userEditRoot {

    }


</style>